import { queryUserMenus, validaterole } from '@/services/auth';
import * as configDictionaryService from '@/services/configDictionary';
import * as rankService from '@/services/rank';
import * as staffService from '@/services/staff';
import * as titleTypeService from '@/services/titleType';
import { message } from 'antd';
import { DEFAULT_RESPONE_SUCCESS, DEFAULT_VALID_STATE, CONST_ROLE_CODE_ADMIN } from '@/constants';

const GlobalModel = {
  namespace: 'global',

  state: {

  },

  subscriptions: {

  },

  effects: {
    *queryUserMenus({ payload }, { call, put }) {
      const { callback } = payload;

      const response = yield call(queryUserMenus);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = [] } = response;
        const userMenus = [];

        result.forEach(item => {
          const { uri = '' } = item;
          userMenus.push(uri);
        });
        if (typeof callback === 'function') {
          callback(userMenus);
        }
        yield put({
          type: 'changeState',
          payload: { userMenus },
        });
      } else {
        // message.error('获取用户菜单列表失败', 1);
      }
    },
    *queryUserPermission({ payload }, { call, put }) {
      const response = yield call(staffService.getCurrentRoleMenuElement, { menuType: '2' });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = [] } = response;
        const userPermissions = [];

        result.forEach(item => {
          const { code = '' } = item;
          userPermissions.push(code);
        });

        yield put({
          type: 'changeState',
          payload: { userPermissions },
        });
      }
    },
    *validaterole({ payload }, { call, put }) {
      const { callback } = payload;

      const response = yield call(validaterole, { roleCode: CONST_ROLE_CODE_ADMIN });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result } = response;

        yield put({
          type: 'changeState',
          payload: { isAdmin: result }
        });
        if (typeof callback === 'function') {
          callback();
        }
      }
    },
    *getCurrentUser({ payload }, { call, put }) {
      const { callback } = payload;

      const response = yield call(staffService.getCurrentUser);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { is_fist_login } = result;
        if (is_fist_login === 1) {
          if (typeof callback === 'function') {
            callback();
          }
        } else {
          yield put({
            type: 'changeState',
            payload: { visible: '首次登录需修改密码' }
          });
        }
      }
    },
    *getCurrentUserUnit({ payload }, { call, put }) {
      const response = yield call(staffService.getCurrentUserUnit);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;

        yield put({
          type: 'changeState',
          payload: { currentUnit: result }
        });
      }
    },
    *queryConfigDictionary({ payload }, { call, put }) {
      const { callback, ...etxra } = payload;

      const response = yield call(configDictionaryService.queryConfigDictionary, etxra);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result } = response;
        if (typeof callback === 'function') {
          callback(result);
        }
      }
    },
    *queryRank({ payload }, { call, put }) {
      const { callback, ...etxra } = payload;

      const response = yield call(rankService.queryData, { pageNo: 1, pageSize: 10000 }, { state: DEFAULT_VALID_STATE });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { content = [] } = result;

        if (typeof callback === 'function') {
          callback(content);
        }
      }
    },
    *queryTitleType({ payload }, { call, put }) {
      const { callback, ...etxra } = payload;

      const response = yield call(titleTypeService.queryData, { pageNo: 1, pageSize: 10000 }, { state: DEFAULT_VALID_STATE });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { content = [] } = result;

        if (typeof callback === 'function') {
          callback(content);
        }
      }
    },
    *resetPassword({ payload }, { call, put }) {
      const { dispatch, oldPassword, newPassword } = payload;

      const response = yield call(staffService.updatePassword, { oldPassword, newPassword });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result } = response;

        if (result === '旧密码不对') {
          message.error('旧密码不正确', 2);
        } else {
          yield put({
            type: 'changeState',
            payload: {
              visible: false
            }
          });
          message.success('密码修改成功，将在3秒后退出请重新登录', 3, () => {
            dispatch({ type: 'login/logout' });
          });
        }
      }
    },
    *queryDataList({ payload }, { call, put }) {
      const { callback, current, pageSize, ...extra } = payload;
      const response = yield call(titleTypeService.queryDataList, { pageNo: current, pageSize }, { ...extra.map });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { resultList = [], rowcount } = result;

        yield put({
          type: 'changeState',
          payload: {
            data: resultList,
            current,
            pageSize,
            total: typeof rowcount === 'string' ? parseInt(rowcount) : rowcount
          }
        });
      }
    },

  },

  reducers: {
    clear(state, { payload }) {
      return {

      };
    },
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    }
  }
};

export default GlobalModel;
